package com.gz.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/cookieLive")
public class CookieLive extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CookieLive被调用");
        //创建一个Cookie,生命周期为60s

        Cookie cookie = new Cookie("job","java");
        //生命周期为-1表示浏览器关闭即销毁，默认值就是-1
        //1.从创建cookie开始计时，60秒后无效
        //2.浏览器根据创建的时间，计时到60秒，就认为改cookie无效
        //3.如果该cookie无效，那么浏览器在发出http请求时，就不再携带改cookie
        cookie.setMaxAge(60); //单位:秒
        response.addCookie(cookie);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<h1>读取cookie信息成功</h1>");
        out.flush();
        out.close();

    }
}
